package cn.aezo.minions.web.controller;

import cn.aezo.minions.common.Result;
import cn.aezo.minions.gen.mapper.TemplateDao;
import cn.aezo.minions.gen.mapper.TemplateItemDao;
import cn.aezo.minions.gen.model.Template;
import cn.aezo.minions.gen.model.TemplateItem;
import cn.aezo.minions.utils.SmUtils;
import cn.aezo.minions.web.services.TemplateService;
import cn.aezo.utils.base.ValidU;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @author smalle
 * @date 2019-08-02 09:11
 */
@RestController
@RequestMapping("/api")
public class TemplateController {
    @Autowired
    HttpServletRequest request;

    @Autowired
    HttpServletResponse response;

    @Autowired
    TemplateDao templateDao;

    @Autowired
    TemplateItemDao templateItemDao;

    @Autowired
    TemplateService templateService;

    /**
     * 获取模板树
     * @param templateId
     * @return
     */
    @RequestMapping(path = "/userTemplateItem")
    public Object findTemplateItem(@RequestParam("templateId") Long templateId) {
        List<Map<String, Object>> templateItemList = templateDao.findTemplateItemList(templateId);
        List<Map<String, Object>> resList = templateService.getLevelData(templateItemList, "0", "id", "pid", 0);

        return Result.success(resList);
    }

    /**
     * 获取模板文件
     * @param templateItemId
     * @return
     */
    @RequestMapping(path = "/userTemplateItem/{templateItemId}")
    public Object getTemplateItem(@PathVariable("templateItemId") Long templateItemId) {
        TemplateItem templateItem = templateItemDao.selectById(templateItemId);

        return Result.success(templateItem);
    }

    /**
     * 添加模板
     * @param multipartFile
     * @param template
     * @return
     */
    @RequestMapping(path = "/editTemplate", method = RequestMethod.POST)
    public Object editTemplate(@RequestParam("file") MultipartFile multipartFile, Template template) {
        String userId = CommonController.getUserId(request);
        File file = null;
        try {
            file = SmUtils.multipartFileToFile(multipartFile);
            return templateService.addTemplate(file, template, Long.valueOf(userId));
        } catch (Exception e) {
            return Result.failure("文件转换出错");
        } finally {
            if(file != null) {
                file.delete();
            }
        }
    }

    /**
     * 保存模板文件代码
     * @param templateItemId
     * @param templateItemValue
     * @return
     */
    @RequestMapping(path = "/saveTemplateItem", method = RequestMethod.POST)
    public Object saveTemplateItem(Long templateItemId, String templateItemValue) {
        return templateService.saveTemplateItem(templateItemId, templateItemValue);
    }

    /**
     * 下载模板压缩文件
     * @param templateId
     * @return
     */
    @RequestMapping(path = "/downloadTemplate", method = RequestMethod.POST)
    public Result downloadTemplate(Long templateId) {
        String userId = CommonController.getUserId(request);
        return templateService.downloadTemplate(templateId, Long.valueOf(userId));
    }

    @RequestMapping(path = "/downloadTemplateFile")
    public void downloadTemplate() {
        String downloadRootDir = System.getProperty("user.dir") + "/runtime/temp";
        request.setAttribute("downloadRootDir", downloadRootDir);
        CommonController.downloadFile(request, response);

        String fileName = (String) request.getAttribute("downloadFileName");
        if(ValidU.isEmpty(fileName)) {
            fileName = request.getParameter("downloadFileName");
        }
        File file = new File(downloadRootDir + "/" + fileName);
        if(file.exists())
            file.delete();
    }
}
